package per.locke.xml.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ReflectUtil {


    public static String getFieldValue(Object o, String fieldName) throws IllegalAccessException, NoSuchFieldException {
        Class objectClass = (Class) o.getClass();

        String value = "";

        Field[] fs = objectClass.getDeclaredFields();
        for (Field field : fs) {

            if (field.getName().equals(fieldName)) {
                field.setAccessible(true);
                value = field.get(o).toString();
                break;
            }

        }

        return value;
    }


    public static String getClassSimpleName(Object o) {
        Class objectClass = o.getClass();
        return objectClass.getSimpleName();
    }

    public static List<String> getFieldNames(Object o) {
        Class objectClass = (Class) o.getClass();
        List<String> names = new ArrayList<>();

        Field[] fs = objectClass.getDeclaredFields();

        for (Field field : fs) {
            names.add(field.getName());
        }

        return names;
    }


}
